<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * class Term_model
 * @author Davinder
 * @version 1.0
 */
 
class Term_model extends CI_Model
{
    private $_tables;
	private $_debug_mode = TRUE;
	const PAGE_SIZE = 100;
	
	public function __construct() {
        parent::__construct();
		
		$this->_tables = new stdClass();
        $this->_tables->terms = 'yb_scrapm_shit_highschooler';
		
		// Load DB if its enabled
		if ( config_item('rest_database_group') )
		{
			$this->db = $this->load->database(config_item('rest_database_group'), TRUE);
		}
	}
    
    public function terms_emojies($p_data=null) {
        
        $this->db->where('term_type',ucfirst($p_data->type));
        
        if ( $p_data->type == 'term' ) {
            $this->db->like('term_word',$p_data->category,'after');
        }
        
        if ( $p_data->random_terms_count > 0 ) {
            $this->db->order_by('RAND()',false);
            $this->db->limit($p_data->random_terms_count);
        }
        
        $query = $this->db->get($this->_tables->terms);
        
        if ( $this->_debug_mode == TRUE ) {
            log_message('DEBUG',__METHOD__.': '.$this->db->last_query());
        }

        if ( $query->num_rows() > 0 ) {
            
            $data = array();
            
            $resultSet = $query->result();
            
            foreach ( $resultSet as $row ) {      
                $data[] = $row;
            }
            
            return $data;
        } else {
            return FALSE;
        }
    }
}